<?php
/**
 * Created by PhpStorm.
 * User: 墨鱼行
 * Signature: 好好学习，天天向上
 * Date: 19-2-18
 * Time: 下午3:26
 */

namespace app\lib\exception;


class ThrowError extends BaseException
{
    // http状态码，404、200
    public $code = 404;

    // 错误码
    public $errorCode = 40000;

    // 错误信息描述
    public $msg = '请求的Banner不存在';

    /**
     * 根据错误码获取对应的文案
     * BannerMissException constructor.
     * @param $errorCode
     * @param array $params
     * @param int $code
     * @param string $msg
     */
    public function __construct ($errorCode, $params = [], $code = 404, $msg = '')
    {
        $codeList = config('code.');
        $this->errorCode = $errorCode;
        $this->code = $code;

        if (!empty($msg))
        {
            $this->msg = $msg;
        }
        else
        {
            if (isset($codeList[$errorCode]))
            {
                $this->msg  = $codeList[$errorCode];
                if ($params)
                {
                    $pairs = [];
                    foreach ($params as $k => $v)
                    {
                        $pairs["{{$k}}"] = $v;
                    }
                    $this->msg  = strtr($this->msg, $pairs);
                }
            }
        }
    }
}